﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SKG.DXF.Ktx.Danhmuc.Phong
{
    public partial class FrmTinhtien : DevExpress.XtraEditors.XtraForm
    {
        public static decimal Tong = 0;
        public string _maphong;
        public Guid _PhongID;
        public Decimal _Gia;

        DAL.Ktx_TienphongDAL _bll = new DAL.Ktx_TienphongDAL();

        DAL.Ktx_SodienDAL _dal = new DAL.Ktx_SodienDAL();
        DAL.Ktx_GiadienDAL _dal1 = new DAL.Ktx_GiadienDAL();

        DAL.Ktx_SonuocDAL _dal2 = new DAL.Ktx_SonuocDAL();
        DAL.Ktx_GianuocDAL _dal3 = new DAL.Ktx_GianuocDAL();

        public FrmTinhtien()
        {
            InitializeComponent();
        }

        private void FrmTinhtien_Load(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            DataTable dtb = _dal.Select(_PhongID, dt);
            DataTable dtb1 = _dal1.Select();
            DataTable dtb2 = _dal2.Select(_PhongID, dt);
            DataTable dtb3 = _dal3.Select();


            int _sodiencu = Convert.ToInt32(dtb.Rows[0]["Socu"]);
            int _sodienmoi = Convert.ToInt32(dtb.Rows[0]["Somoi"]);

            int _sonuoccu = Convert.ToInt32(dtb2.Rows[0]["Socu"]);
            int _sonuocmoi = Convert.ToInt32(dtb2.Rows[0]["Somoi"]);

            int _giadien = Convert.ToInt32(dtb1.Rows[0]["Gia"]);
            int _gianuoc = Convert.ToInt32(dtb3.Rows[0]["Gia"]);

            int _SoKW = _sodienmoi - _sodiencu;
            int _Sokhoi = _sonuocmoi - _sonuoccu;

            Decimal _tongtien = _SoKW * _giadien + _Sokhoi * _gianuoc + _Gia;
            Tong = _tongtien;
            
            txtPhong.Text = _maphong;
            txtTienphong.Text = _Gia+ "";
            txtTiendien.Text = (_SoKW * _giadien) + "";
            txtTiennuoc.Text = (_Sokhoi * _gianuoc) + "";
            txtSotien.Text = _tongtien + "";
            txtBangchu.Text = Current.ToString(_tongtien);
            txtNgaythu.Text = DateTime.Now + "";            
        }

        private void btnInHD_Click(object sender, EventArgs e)
        {
            DataTable tmp = _bll.Select_Max();
            string _sophieu = Current.AddID(tmp.Rows[0]["So"] + "");

            var o = new DAL.Entities.Ktx_Tienphong()
            {
                Sophieu = _sophieu,
                Tenphieu = "Thu tiền phòng",
                PhongId = _PhongID,
                Ngaythu = DateTime.Now,
                Sotien = Tong,
                Bangchu = Current.ToString(Tong)
            };

            var oki = _bll.Insert(o);
            if (oki == null) XtraMessageBox.Show("Lỗi không lưu được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                Report.FrmRp_Tienphong frm = new Report.FrmRp_Tienphong();
                frm._phongID = _PhongID;
                frm.Show();
                this.Close();
            }
        }

        private void btnLuulai_Click(object sender, EventArgs e)
        {
            DataTable tmp = _bll.Select_Max();
            string _sophieu = Current.AddID(tmp.Rows[0]["So"] + "");

            var o = new DAL.Entities.Ktx_Tienphong()
            {
                Sophieu = _sophieu,
                Tenphieu = "Thu tiền phòng",
                PhongId = _PhongID,
                Ngaythu = DateTime.Now,
                Sotien = Tong,
                Bangchu = Current.ToString(Tong)
            };

            var oki = _bll.Insert(o);
            if (oki == null) XtraMessageBox.Show("Lỗi không lưu được", "Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
            else
            {
                DialogResult result = XtraMessageBox.Show("Lưu lại thành công !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                    this.Close();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}



















